//
//  PersonManagerViewController.m
//  CityFire
//
//  Created by liuyangzhu on 2017/8/3.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "PersonManagerViewController.h"
#import "PersonInfoViewController.h"
#import "PersonManageTableViewCell.h"
#import "PersonInfoViewModel.h"

@interface PersonManagerViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, weak) IBOutlet UITableView *personTableView;
@property (nonatomic, weak) IBOutlet UISegmentedControl   *segmentedController;
@property (nonatomic, strong) PersonInfoViewModel *personInfoViewModel;

@end

@implementation PersonManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = kPersonManager_TITLE;
    
    [self bindRAC];
    
    [self getPersonInfo:0 page:1 pageNo:40];
}


/**
 选择用户类型

 @param sender sender
 */
- (IBAction)selectPersonIndex:(UISegmentedControl *)sender
{
    [self getPersonInfo:sender.selectedSegmentIndex page:1 pageNo:40];
}

- (void)getPersonInfo:(NSInteger)index page:(NSInteger)page pageNo:(NSInteger)pageNo
{
    [self.personInfoViewModel getQueryAdmins:@{@"status" : index == 2 ? @"" : index == 0 ? @(1)  : @(0), @"pageNo" : @(1), @"pageSize" : @(20)}];
}

/**
 绑定监测数据源
 */
- (void)bindRAC
{
    [RACObserve(self.personInfoViewModel, personsArray) subscribeNext:^(NSArray *personArray) {
        if (personArray) {
            [self.personTableView reloadData];
        }
    }];
}

- (PersonInfoViewModel *)personInfoViewModel
{
    if (_personInfoViewModel == nil) {
        _personInfoViewModel = [[PersonInfoViewModel alloc] init];
    }
    
    return _personInfoViewModel;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.personInfoViewModel.personsArray count];
}
   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PersonManageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonManageTableViewCell"];
    
    PersonInfoModel *pInfoModel = self.personInfoViewModel.personsArray[indexPath.section];
    cell.personInfoModel = pInfoModel;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    PersonInfoViewController *infoVC = kVCFromSB(kPersonalStoryBoard, PersonInfoViewController);
    PersonInfoModel *pInfoModel = self.personInfoViewModel.personsArray[indexPath.section];
    infoVC.personInfoModel = pInfoModel;
    
    [self.navigationController  pushViewController:infoVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
